import UIKit
import CoreImage
import CoreImage.CIFilterBuiltins

class Image {
    static let shared = Image()
    private init() {}
    
    func applyFilter(to image: UIImage, filterType: FilterType) -> UIImage? {
        guard let cgImage = image.cgImage else { return nil }
        
        let context = CIContext()
        let ciImage = CIImage(cgImage: cgImage)
        
        let filter: CIFilter
        switch filterType {
        case .none:
            return image
        case .sepia:
            filter = CIFilter.sepiaTone()
            filter.setValue(ciImage, forKey: kCIInputImageKey)
            filter.setValue(1.0, forKey: kCIInputIntensityKey)
        case .noir:
            filter = CIFilter.photoEffectNoir()
            filter.setValue(ciImage, forKey: kCIInputImageKey)
        case .mono:
            filter = CIFilter.photoEffectMono()
            filter.setValue(ciImage, forKey: kCIInputImageKey)
        case .chrome:
            filter = CIFilter.photoEffectChrome()
            filter.setValue(ciImage, forKey: kCIInputImageKey)
        }
        
        guard let outputImage = filter.outputImage,
              let cgImg = context.createCGImage(outputImage, from: outputImage.extent) else {
            return nil
        }
        
        return UIImage(cgImage: cgImg)
    }
    
    enum FilterType {
        case none
        case sepia
        case noir
        case mono
        case chrome
    }
}